﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class UserEdit : Form
    {
        public string originname = "";               // 原来的名字
        public string originpassword = "";           // 原来的密码
        public string originlimit = "";              // 原来的权限
        public UserEdit()
        {
            InitializeComponent();
            comboBox_limit.Items.Add("user");
            comboBox_limit.Items.Add("admin");
            comboBox_limit.Items.Add("none");
        }

        public void Fill_Info()
        {
            textBox_username.Text  = originname;
            textBox_password.Text  = originpassword;
            comboBox_limit.Text  = originlimit;
        }


        private void UserEdit_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 更新信息
                string updatename = textBox_username.Text.Trim();
                string updatepassword = textBox_password.Text.Trim();
                string updatelimit = comboBox_limit.Text.Trim();

                if (updatename == "") { MessageBox.Show("用户名不能为空！"); return; }
                if (updatepassword == "") { MessageBox.Show("密码不能为空！"); return; }
                if (updatelimit == "") { MessageBox.Show("必须选择权限！"); return; }

                //DataSet ds = MainView.mysql.sql_search_database("select * from usertable");
                //int rowcount = ds.Tables[0].Rows.Count;
                
                // 判断此名是否存在
               bool right=MainView.mysql.excute_sql("update usertable set username='"+updatename+"', password='"+updatepassword+"', pression='"+updatelimit+"' where username='"+updatename+"'");
               if (right == false) { MessageBox.Show("更新过程中出现错误！"); }
               this.Dispose();
            }
            catch { MessageBox.Show("修改失败！查看是否有重名！"); }
        }
    }
}
